const express = require('express');
const router = express.Router();
const Address = require('../sql/collection/Address')
const sql = require('../sql/index')
const uuid = require('node-uuid')
const utils = require('../utils/index');

router.get('/', function (req, res, next) {
    res.send()
});
/**
* @api {post} /api/address/add 添加地址
* @apiName PostApiAddressAdd
* @apiGroup Address
* 
* @apiParam { Object } address 地址信息
* 
* @apiSuccessExample { json } Success-Response:
        res.send({
            code: '200',
            message: '添加地址'
        })
* @apiSampleRequest /api/address/add
* @apiVersion 0.0.0
*/
router.post('/add', (req, res, next) => {
    utils.verifyToken(req, res).then(() => {
        const { address } = req.body
        const insertData = {
            ...address,
            addressid: "address_" + uuid.v1(),
            time: Date.now()
        }
        if (address.isDefault) {
            sql.update(Address, { userid: address.userid }, { $set: { isDefault: false } }, 1).then(() => {
                sql.insert(Address, insertData).then(() => {
                    res.send({
                        code: '200',
                        message: '添加地址'
                    })
                })
            })
        } else {
            sql.insert(Address, insertData).then(() => {
                res.send({
                    code: '200',
                    message: '添加地址'
                })
            })
        }
    })
})

/**
* @api {post} /api/address/default 获取默认地址
* @apiName PostApiAddressDefault
* @apiGroup Address
* 
* @apiParam { String } userid 用户ID
* 
* @apiSuccessExample { json } Success-Response:
        res.send({
            code: '200',
            message: '获取默认地址'
        })
* @apiSampleRequest /api/address/default
* @apiVersion 0.0.0
*/
// 获取默认地址
router.post('/default', (req, res, next) => {
    utils.verifyToken(req, res).then(() => {
        const { userid } = req.body
        sql.find(Address, { userid, isDefault: true }, { _id: 0 }).then(data => {
            res.send({
                code: '200',
                message: '获取默认地址',
                data
            })
        })
    })
})

/**
* @api {post} /api/address/addresslist 获取地址列表
* @apiName PostApiAddressList
* @apiGroup Address
* 
* @apiParam { String } userid 用户ID
* 
* @apiSuccessExample { json } Success-Response:
        res.send({
            code: '200',
            message: '获取地址列表'
        })
* @apiSampleRequest /api/address/addresslist
* @apiVersion 0.0.0
*/
// 获取地址列表
router.post('/addresslist', (req, res, next) => {
    utils.verifyToken(req, res).then(() => {
        const { userid } = req.body
        sql.sort(Address, { userid }, { _id: 0 }, { time: -1}).then(data => {
            res.send({
                code: '200',
                message: '获取地址列表',
                data
            })
        })
    })
})
/**
* @api {post} /api/address/updatedefaultaddress 修改默认地址
* @apiName PostApiupdateDefaultAddress
* @apiGroup Address
* 
* @apiParam { String } userid 用户ID
* @apiParam { String } time 地址的唯一标识（创建时间）
* 
* @apiSuccessExample { json } Success-Response:
        res.send({
            code: '200',
            message: '修改默认地址'
        })
* @apiSampleRequest /api/address/updatedefaultaddress
* @apiVersion 0.0.0
*/
// 修改默认地址
router.post('/updatedefaultaddress', (req, res, next) => {
    utils.verifyToken(req, res).then(() => {
        const { userid, time } = req.body
        sql.update(Address, { userid, isDefault: true }, { $set: { isDefault: false } }).then(() => {
            sql.update(Address, { userid, time }, { $set: { isDefault: true } }).then(() => {
                res.send({
                    code: '200',
                    message: '修改默认地址'
                })
            })
        })
    })
})
/**
* @api {post} /api/address/getaddress 获取指定地址信息
* @apiName PostApiGetAddress
* @apiGroup Address
* 
* @apiParam { String } userid 用户ID
* @apiParam { String } time 地址的唯一标识（创建时间）
* 
* @apiSuccessExample { json } Success-Response:
        res.send({
            code: '200',
            message: '获取地址信息'
        })
* @apiSampleRequest /api/address/getaddress
* @apiVersion 0.0.0
*/
// 获取地址信息
router.post('/getaddress', (req, res, next) => {
    utils.verifyToken(req, res).then(() => {
        const { userid, time } = req.body
        sql.find(Address, { userid, time }, { _id: 0 }).then(data => {
            res.send({
                code: '200',
                message: '获取地址信息',
                data
            })
        })
    })
})
/**
* @api {post} /api/address/updateaddress 修改指定地址
* @apiName PostApiupdateAddress
* @apiGroup Address
* 
* @apiParam { String } userid 用户ID
* @apiParam { String } time 地址的唯一标识（创建时间）
* 
* @apiSuccessExample { json } Success-Response:
        res.send({
            code: '200',
            message: '修改指定地址'
        })
* @apiSampleRequest /api/address/updateaddress
* @apiVersion 0.0.0
*/
// 修改指定地址
router.post('/updateaddress', (req, res, next) => {
    utils.verifyToken(req, res).then(() => {
        const { userid, time, info } = req.body
            if (info.isDefault) {
                sql.update(Address, { userid, isDefault: true }, { $set: { isDefault: false } }).then(() => {
                    sql.update(Address, { userid, time }, { $set: { ...info } }).then(() => {
                        res.send({
                            code: '200',
                            message: '修改指定地址'
                        })
                    })
                })
            } else {
                sql.update(Address, { userid, time }, { $set: { ...info } }).then(() => {
                    res.send({
                        code: '200',
                        message: '修改指定地址'
                    })
                })
            }
    })
})
module.exports = router;
